#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <pthread.h>

void* func_1(void* arg) {
	int cancelstate;
	
	while (1) {
		pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
		sleep(2);
		pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
		pthread_testcancel();
		printf("run\n");
	}
}

int main() {
	pthread_t tid;
	pthread_create(&tid, NULL, func_1, "thread 1");
	sleep(8);
	pthread_cancel(tid);
	pthread_join(tid, NULL);

	printf("main exit\n");
	return 0;
}